<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template #territory_area="{row}">
      <p>{{row.value}}</p>
      <div class="edit_territory_area" v-if="in_right_admin('User/edit_territory_area_id')">
        <div class="flex">
          <select_one :config="territory_area.config1" v-model:value="territory_area.value1"></select_one>
          <select_one :config="territory_area.config2" v-model:value="territory_area.value2"></select_one>
          <select_one :config="territory_area.config3" v-model:value="territory_area.value3"></select_one>
          <el-button type="primary" @click="edit_territory_area_id">修改代理区域</el-button>
        </div>
      </div>
    </template>
  </forms>
</template>

<script>
import select_one from "@/components/auto_import/UnifyParam/select_one.vue";
import s94Mjs from "s94-js";
import {_config, decode_config} from "@/js/s94_tool.js";
import {ajax} from "@/js/api_tool.js";
import {iframe, in_right_admin} from "@/js/tool.js";
export default {
  components: {select_one},
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let sex_config = _config('status_alias_map.user.sex');
    let forms_config = {

      phone: {label:'手机号码',name:'phone',type:'text_one',tips:''},
      nickname: {label:'昵称',name:'nickname',type:'text_one',tips:''},
      truename: {label:'真实姓名',name:'truename',type:'text_one',tips:''},
      password: {label:'登录密码',name:'password',type:'text_one',tips:id?'为空表示不修改':''},
      head_img: {label:'头像',name:'head_img',type:'file_img',tips:'用户头像，建议尺寸100*100'},
      sex: {label:'性别',name:'sex',type:'radio',config:sex_config,tips:'',value:0},
      info: {label:'简介',name:'info',type:'text_more',tips:''},
      area_ids: {label:'所在区域',name:'area_ids',type:'area_4l',tips:''},
      parent_id: {label:'上级用户',name:'parent_id',type:'list_id',config:'user/index',tips:'',value:0},
      level: {label:'等级',name:'level',type:'select_one',config:[],value:0,tips:''},
      territory_area_title:{label:'代理区域', name:'territory_area_title', type:"territory_area"},
    }
    if (id){
      forms_config = Object.assign({
        encode_id: {label:'用户编号',name:'encode_id'}
      },forms_config);
      Object.assign(forms_config, {
        money: {label:'余额',name:'money'},
        score: {label:'积分',name:'score'},
        openid: {label:'微信openid',name:'openid',type:'json_param', is_info:true},
        vip_expiration_time: {label:'VIP到期时间',name:'vip_expiration_time'},
        last_ip: {label:'最后登录IP',name:'last_ip'},
        last_time: {label:'最后登录时间',name:'last_time'},
      })
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,

      territory_area: {
        config1: [],
        config2: [],
        config3: [],
        value1: "",
        value2: "",
        value3: "",
      },
    }
  },
  methods: {
    in_right_admin: in_right_admin,
    onsubmit(formData){
      let url = this.id ? `/admin/user/edit?id=`+this.id : '/admin/user/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('user/index', '$refs.list_url.list');
          iframe.hide('user/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    },
    get_children(id, callback){
      ajax({url:'/home/api/area?parent_id='+id, cache:true}, function (res){
        callback(res.data.list);
        return false;
      })
    },
    edit_territory_area_id(){
      let app = this;
      let territory_area_id = 0;
      if (this.territory_area.value1) territory_area_id = this.territory_area.value1;
      if (this.territory_area.value2) territory_area_id = this.territory_area.value2;
      if (this.territory_area.value3) territory_area_id = this.territory_area.value3;
      ajax({url:'/admin/user/edit_territory_area_id?id='+this.id, data:{territory_area_id:territory_area_id}}, function (res){
        if(res.data>0) app.init();
      })
    },
    init(){
      ajax({url:'/admin/user/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('user/edit');
        }else {
          this.$router.push('index');
        }
      })
    }
  },
  watch:{
    'territory_area.value1'(v){
      this.territory_area.value2 = "";
      if (s94Mjs.to_string(v)===""){
        this.territory_area.config2 = [];
      }else {
        this.get_children(v, (list=>{
          this.territory_area.config2 = decode_config(list, {content_key:'name', value_key:'id'});
        }))
      }
      console.log('territory_area.value1', [this.territory_area.value1,this.territory_area.value2,this.territory_area.value3]);
    },
    'territory_area.value2'(v){
      this.territory_area.value3 = "";
      if (s94Mjs.to_string(v)===""){
        this.territory_area.config3 = [];
      }else {
        this.get_children(v, (list=>{
          this.territory_area.config3 = decode_config(list, {content_key:'name', value_key:'id'});
        }))
      }
      console.log('territory_area.value2', [this.territory_area.value1,this.territory_area.value2,this.territory_area.value3]);
    },
    'territory_area.value3'(v){
      console.log('territory_area.value3', [this.territory_area.value1,this.territory_area.value2,this.territory_area.value3]);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      this.init();
    }

    //其他初始化操作
    ajax({url:'/admin/user_level/list?order[id]=asc',cache:true},(res)=>{
      this.config.level.config = decode_config(res.data.list, {content_key:'name', value_key:'level'});
      return false;
    })

    this.get_children(0, (list=>{
      this.territory_area.config1 = decode_config(list, {content_key:'name', value_key:'id'});
    }))
  }
}
</script>
